package personal.lijun.skeleton.springcloud.serviceprovider.service.controller;

import org.springframework.web.bind.annotation.*;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.AvailableResource;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.Resource;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.ResourcePermission;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.service.ResourceService;
import personal.lijun.skeleton.springcloud.utils.validate.WrapApiResult;

import java.util.List;

/**
 * @Auther: lijun@sstcsoft.com
 * @Date: 2019-10-11 15:54
 */
@RestController
@RequestMapping("/resources")
public class ResourceController {
    @javax.annotation.Resource
    private ResourceService resourceService;

    @GetMapping("")
//    @WrapApiResult
    public List<Resource> listResource(@RequestParam() int routerId){
        return this.resourceService.listByRouterId(routerId);
    }

    @GetMapping("/{id}")
//    @WrapApiResult
    public Resource getById(@PathVariable("id") int id) {
        return this.resourceService.getById(id);
    }

    @PutMapping("/{id}")
//    @WrapApiResult
    public Integer updateResource(@RequestBody Resource resource) {
        return  this.resourceService.updateResource(resource);
    }

    @PostMapping("")
    public Resource saveResource(@RequestBody Resource resource){
        this.resourceService.saveResource(resource);
        return resource;
    }

    @DeleteMapping("/{id}")
    public Integer removeResource(@PathVariable("id") int id){
        return this.resourceService.removeResource(id);
    }

    @GetMapping("/permissions")
    public List<ResourcePermission> listResourcePermission(
            @RequestParam() Short userId, @RequestParam(required = false) int routerId){
        return this.resourceService.listPermission(userId,routerId);
    }

    @GetMapping("/available")
    public List<AvailableResource> listAvailableResource() {
        return this.resourceService.listAllAvailable();
    }
}
